//
//  CaiPiaoConfirm2ViewController.m
//  EasyToo
//
//  Created by linshengqin on 13-8-24.
//
//

#import "CaiPiaoConfirm2ViewController.h"
#import "NetworkAlertView.h"

@interface CaiPiaoConfirm2ViewController ()

@end

@implementation CaiPiaoConfirm2ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)initData
{
    caipiao_model = [CaiPiaoModel sharedata];
    times = caipiao_model.times;
    if(times<=0) times=1;
    siglePrice = 2;
    payment = caipiao_model.payType;
    
    [CaipiaoManager shareManager].delegate = self;

}
-(void) initScrollView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self.view addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 1000);
    _bgScrollView.showsVerticalScrollIndicator = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initData];
    [self initScrollView];
	// Do any additional setup after loading the view.
    UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 300, 340)];
    imageView.image = [UIImage imageNamed: @"list2" ];
    //    UIImageView *imageView = [[UIImageView alloc] initWithImage:image] ;
    [_bgScrollView addSubview:imageView];
    
    UILabel *label_message = [[UILabel alloc] initWithFrame:CGRectMake(110, 10, 150, 30)];
    label_message.text = @"确认信息";
    label_message.textColor = [UIColor redColor];
    label_message.backgroundColor = [UIColor clearColor];
    label_message.font = [UIFont boldSystemFontOfSize:20.0f];  //UILabel的字体大小

    [_bgScrollView addSubview:label_message];
    
    int x = 50;
    
    UILabel *label_numbers = [[UILabel alloc] initWithFrame:CGRectMake(30, x, 250, 30)];
    [label_numbers setText:[NSString stringWithFormat:@"投注号 : %@",@""]];
    label_numbers.font = [UIFont systemFontOfSize:13];
    label_numbers.backgroundColor = [UIColor clearColor];
    [_bgScrollView addSubview:label_numbers];
    
    UILabel *label_account = [[UILabel alloc] initWithFrame:CGRectMake(30, x+30, 250, 30)];
    [label_account setText:[NSString stringWithFormat:@"注数 : %ld注",caipiao_model.totalCount]];
    label_account.font = [UIFont systemFontOfSize:13];
    label_account.backgroundColor = [UIColor clearColor];

    [_bgScrollView addSubview:label_account];
    
    
    UILabel *label_price = [[UILabel alloc] initWithFrame:CGRectMake(30, x+60, 250, 30)];
    [label_price setText:[NSString stringWithFormat:@"倍投 : %d倍",times]];
    label_price.font = [UIFont systemFontOfSize:13];
    label_price.backgroundColor = [UIColor clearColor];

    [_bgScrollView addSubview:label_price];
    
    UILabel *label_pay = [[UILabel alloc] initWithFrame:CGRectMake(30, x+90, 250, 30)];
    [label_pay setText:[NSString stringWithFormat:@"单价 : %d元",siglePrice]];
    label_pay.font = [UIFont systemFontOfSize:13];
    label_pay.backgroundColor = [UIColor clearColor];

    [_bgScrollView addSubview:label_pay];
    
    UILabel *label_payment = [[UILabel alloc] initWithFrame:CGRectMake(30, x+120, 250, 30)];
    [label_payment setText:[NSString stringWithFormat:@"支付方式 : %@",@"银联"]];
    label_payment.font = [UIFont systemFontOfSize:13];
    label_payment.backgroundColor = [UIColor clearColor];

    [_bgScrollView addSubview:label_payment];
    
    
    UILabel *label_total = [[UILabel alloc] initWithFrame:CGRectMake(170, x+180, 150, 30)];
    [label_total setText:[NSString stringWithFormat:@"合计:%ld元",caipiao_model.totalPrice]];
    label_total.font = [UIFont boldSystemFontOfSize:20.0f];  //UILabel的字体大小
    label_total.backgroundColor = [UIColor clearColor];

    label_total.textColor = [UIColor redColor];
    [_bgScrollView addSubview:label_total];
    //
    /*
    UITextField* id_field = [[[UITextField alloc] initWithFrame:CGRectMake(30, 340.0, 250, 40)]autorelease];
    [id_field setBorderStyle:UITextBorderStyleBezel];
    id_field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    id_field.placeholder = @"身份证号";
    id_field.borderStyle = UITextBorderStyleRoundedRect;
    id_field.delegate = self;
    id_field.returnKeyType = UIReturnKeyDone;
    id_field.layer.borderWidth = 1 ;
    id_field.layer.cornerRadius = 10 ;
    [_bgScrollView addSubview:id_field];
     */
    //下一步按钮
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [nextButton setFrame:CGRectMake(40, 340, 240, 40)];
    
    [nextButton setTitle:@"确认支付" forState:UIControlStateNormal];        //设置button在没有选中的时候显示的字体
    
    nextButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];         //设置button显示字体的大小
    
    [nextButton setBackgroundImage:[UIImage imageNamed:@"next1"] forState:UIControlStateNormal];    //设置button背景显示图片
    
    [_bgScrollView addSubview:nextButton];
    
    [nextButton addTarget:self action:@selector(confirmMessge:) forControlEvents:UIControlEventTouchUpInside];
	// Do any additional setup after loading the view.
}

- (void)dealloc
{
    [CaipiaoManager shareManager].delegate = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)confirmMessge:(id)sender
{
    if ([caipiao_model.identify length] <= 0) //身份
    {
        UIAlertView* baseAlert = [[[UIAlertView alloc] initWithTitle:@"下注失败,请先登陆或检查身份证信息" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil] autorelease];
        [baseAlert show];
        return;
    }
    
    [[NetworkAlertView shareView] showNetView];
    
    printf("11111\n");
    NSMutableArray* array = [NSMutableArray arrayWithArray:caipiao_model.stakeList];
    NSString* betCount = [NSString stringWithFormat:@"%d",number];
    NSString* betMoney = [NSString stringWithFormat:@"%ld",price];
    NSString* bs = [NSString stringWithFormat:@"%d",times];
    NSString* lType = [NSString stringWithFormat:@"%d",caipiao_model.lType];
    
    [[CaipiaoManager shareManager] caipiaoOrder:array BType:@"0" BetCount:betCount BetMoney:betMoney BS:bs isAppend:@"0" Issue:caipiao_model.issue LType:lType PType:@"0" allMoney:[NSString stringWithFormat:@"%ld",caipiao_model.totalPrice] CardID:caipiao_model.identify];
    
    
}

- (void)onOrderCaipiaoFinish:(BOOL)isSuccess Tn:(NSString *)tn
{
    [[NetworkAlertView shareView] delView];
    
    [UPPayPlugin startPay:tn sysProvide:nil spId:nil mode:PAY_MODE_REALEASE viewController:self delegate:self];
}

-(void)UPPayPluginResult:(NSString*)result
{
    NSString *alertString = nil;
    if ([result isEqualToString:@"success"])
    {
        alertString = @"交易成功";
    }
    else if ([result isEqualToString:@"cancel"])
    {
        alertString = @"交易取消";
    }
    else
    {
        alertString = @"交易失败";
    }
    
    UIAlertView *alert = [[UIAlertView alloc]  initWithTitle:@"支付结果" message:alertString  delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.view.frame.size.width;
    float height = self.view.frame.size.height;
    if(offset > 0)
    {
        CGRect rect = CGRectMake(0.0f, -offset,width,height);
        self.view.frame = rect;
    }
    [UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    _identify = textField.text;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
    self.view.frame = rect;
    [UIView commitAnimations];
    [textField resignFirstResponder];
    return YES;
}
@end
